package com.saleSystem.controller;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.saleSystem.pojo.Content;
import com.saleSystem.pojo.TradeHistory;
import com.saleSystem.pojo.User;
import com.saleSystem.service.IProductService;
import com.saleSystem.service.ITradeHistoryService;
import com.saleSystem.service.IUserService;
import com.saleSystem.vo.Product;

/**
 * 产品查看展示控制
 * @author unionpay
 *
 */
@Controller("productController")
@RequestMapping("/")
public class ProductController {

	private static final Logger logger = LoggerFactory.getLogger(ProductController.class);
	
	@Autowired
	private IProductService iProductService;
	
	@Autowired
	private IUserService iUserService;
	
	@Autowired
	private ITradeHistoryService iTradeHistoryService;

	
	/**
	 * 产品查看
	 * @param id
	 * @param map
	 * @param session
	 * @return
	 */
	@RequestMapping(value="show",method= RequestMethod.GET)
	public String show(@RequestParam(value = "id")Integer id,ModelMap map,HttpSession session){
		
		
		Content content = iProductService.getContent(id);

		User user = (User)session.getAttribute("user");
		TradeHistory buyerTradeHistory = new TradeHistory();
		TradeHistory sellerTradeHistory = new TradeHistory();
		
		
		if(user != null){
			if(user.getUsertype() == 0){
			
				 buyerTradeHistory = iTradeHistoryService.getBuyInfo(user.getId(),id);
			}else{
				
				 sellerTradeHistory = iTradeHistoryService.getSellInfo(id);
			}
		}
		Product product = ConvertToProduct(content,buyerTradeHistory,sellerTradeHistory);
		map.addAttribute("product",product);
		map.addAttribute("user",user);
		return "show";
	}
	
	
	private Product ConvertToProduct(Content content,TradeHistory buyerTradeHistory,TradeHistory sellerTradeHistory){
		
		Product product = new Product();
		if(content != null){
			product.setId(content.getId());
			product.setImage(content.getImage());
			product.setPrice(content.getPrice());
			product.setSummary(content.getSummary());
			product.setTitle(content.getTitle());
			product.setDetail(content.getDetail());
		}
		
		if(buyerTradeHistory != null){
			product.setBuy(true);
			product.setBuyNum(buyerTradeHistory.getBuynum());
		}else{
			product.setBuy(false);
			product.setBuyNum(0);
		}
		
		if(sellerTradeHistory != null){
			product.setSell(true);
			product.setSaleNum(sellerTradeHistory.getBuynum());
		}else{
			product.setSell(false);
			product.setSaleNum(0);
		}
		
		return product;
	}
	
	
	/**
	 * 展示商品
	 * @param map
	 * @param session
	 * @param request
	 * @param type
	 * @return
	 */
	@RequestMapping(value={"index",""},method= RequestMethod.GET)
	public String display(ModelMap map,HttpSession session,HttpServletRequest request,
			@RequestParam(value = "type",required = false)Integer type){
		
		List<Content> contentList = iProductService.getAllContent();

		User user = (User)session.getAttribute("user");
		if(user!=null){
			logger.info("用户类型:" + user.getUsertype() + ",用户名：" + user.getUsername());

		}
		
		int listType = 0;
		if(user != null && user.getUsertype() == 0 && type != null && type == 1){
			listType = 1;
		}
	
		List<Product> productList = new ArrayList<Product>();
		for(Content content : contentList){
			
			TradeHistory buyerTradeHistory = new TradeHistory();
			TradeHistory sellerTradeHistory = new TradeHistory();
			if(user != null){
				if(user.getUsertype() == 0){
				
					 buyerTradeHistory = iTradeHistoryService.getBuyInfo(user.getId(),content.getId());
				}else{
					
					 sellerTradeHistory = iTradeHistoryService.getSellInfo(content.getId());
				}
			}
			
			Product product = ConvertToProduct(content,buyerTradeHistory,sellerTradeHistory);
			
			//区分是展示全部还是未购买产品
			if(type != null && type == 1){
				if(!product.getIsBuy()){
					productList.add(product);
				}
			}else{
				productList.add(product);
			}
		}
		map.addAttribute("productList", productList);
		map.addAttribute("user", user);
		map.addAttribute("listType", listType);
		return "index";
	}
	
	
}
